package implementation.sort.MergeSort;


import anaydis.sort.SorterType;
import implementation.sort.AbstractSorter;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public abstract class AbstractMergeSort extends AbstractSorter {


    protected AbstractMergeSort(SorterType sorterType) {
        super(sorterType);
    }


    public <T> void merge(Comparator<T> comparator, List<T> list, int start, int half, int end) {
        int k;
        int j;
        List<T> aux = new ArrayList<T>();

        for (int i = 0; i < list.size(); i++) {
            aux.add(list.get(i));
        }
        for (j = half + 1; j > start; j--) {
            aux.set(j - 1, list.get(j - 1));
        }
        for (k = half; k < end; k++) {
            aux.set(end + half - k, list.get(k + 1));
        }
        for (int h = start; h <= end; h++) {
            if (greater(comparator, aux, j, k)) {
                list.set(h, aux.get(k--));
            } else {
                list.set(h, aux.get(j++));
            }
        }
    }
/*    public <T> List<T> merge(Comparator tComparator, List<T> list1, List<T> list2) {
        int start1 = 0;
        int start2 = 0;
        int end1 = list1.size() - 1;
        int end2 = list2.size() - 1;
        List<T> aux = new ArrayList<T>();

        while (start1 <= end1 && start2 <= end2) {
            if (tComparator.compare(list1.get(start1), list2.get(start2))<0) {
                aux.add(list1.get(start1));
                start1++;
            } else {
                aux.add(list2.get(start2));
                start2++;
            }
        }

        if (start1 > end1) {
            for (int i = start2; i <= end2; start2++) {
                aux.add(list2.get(i));
            }
        } else {
            for (int j = start1; j <= end1; j++) {
                aux.add(list1.get(j));
            }
        }
        return aux;
    }*/


}
